package com.example.springmvc_study;

import com.example.springmvc_study.model.User;
import com.sun.net.httpserver.HttpsServer;
import com.sun.org.apache.xpath.internal.operations.Mult;
import jdk.nashorn.internal.ir.RuntimeNode;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.management.Agent;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName RequestController
 * @Description TODO
 * @Author H.
 * @Date 2023/4/25 19:01
 * @Version 1.0
 */
@RestController
@RequestMapping("/red")
public class RequestController {
    @RequestMapping("/queryStudent/{id}")
    public Object queryStudent(@PathVariable Integer id){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        return  map;
    }

    @RequestMapping("/header")
    public Object header(@RequestHeader("User-Agent") String userAgent){
        Map<String,Object> map = new HashMap<>();
        map.put("User-Agent",userAgent);
        return  map;
    }

    @RequestMapping("/cookie")
    public Object cookie(@CookieValue("My-Cookie") String cookie){
        Map<String,Object> map = new HashMap<>();
        map.put("My-Cookie",cookie);
        return  map;
    }

    @RequestMapping("/login")
    public Object login(HttpServletRequest req){
        HttpSession session = req.getSession();
        session.setAttribute("user","123456");
        Map<String,Object> map = new HashMap<>();
        map.put("user","123456");
        return  map;
    }

    @RequestMapping("/getSession")
    public Object getSession(@SessionAttribute("user") String user){
        Map<String,Object> map = new HashMap<>();
        map.put("session中User",user);
        return  map;
    }

    //======================获取请求数据
    //通过简单类型  无注解

    @RequestMapping("/log")
    public Object log(String username, Integer age, int weight){
        Map<String,Object> map = new HashMap<>();
        map.put("user",username);
        map.put("age",age);
        map.put("weight",weight);
        return  map;
    }

    @RequestMapping("/complex")
    public Object complex(User user){
        return user;
    }

    @RequestMapping("/rp")
    public Object rp(@RequestParam String username, @RequestParam String email ,@RequestParam Integer age){
        Map<String,Object> map = new HashMap<>();
        map.put("username",username);
        map.put("email",email);
        map.put("age",age);
        return  map;
    }

    @RequestMapping("/upload")
    public Object upload(MultipartFile head) throws IOException {
        Map<String,Object> map = new HashMap<>();
        map.put("filename",head.getOriginalFilename());
        map.put("content", new String(head.getBytes(), "utf-8"));
        map.put("size",head.getSize());
        return  map;
    }
}
